
from Common.ReadYaml import ConfigYaml
import requests
from Common.ReadWriteIni import ReadWrite


class Login:

    content_type_key = ConfigYaml("type_key").base_config
    content_json_type_value = ConfigYaml("json_type").base_config
    content_form_type_value = ConfigYaml("form_type").base_config
    headers = {content_type_key: content_json_type_value}
    base_url = ConfigYaml("domain_name").base_url
    token_key = ConfigYaml("token_key_value").base_config

    def __init__(self):
        self.login_url = ConfigYaml("login_api").base_config
        self.url = self.base_url + self.login_url
        self.login_user = ConfigYaml("login_user").base_config
        self.login_pwd = ConfigYaml("login_pwd").base_config
        self.data = {"username": self.login_user, "password": self.login_pwd}
    def get_token(self):
        r = requests.post(url=self.url, json=self.data, headers=self.headers, stream=True)
        if r.json().get(self.token_key):
            return r.json().get(self.token_key)

    def write_token(self):
        token = self.get_token()
        ReadWrite(value=token).write_ini_token()


if __name__ == "__main__":
    Login().write_token()





